<template>
    <div class="register">
        <van-form ref="loginForm" class="register-form" @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="loginParams.tel" name="账号" label="账号" placeholder="手机号/邮箱/用户名" :rules="telRules" />
                <van-field v-model="loginParams.password" type="password" name="密码" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码', trigger: 'onBlur' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { closeToast, showSuccessToast, showFailToast, showLoadingToast } from 'vant';
import { doLoginAPI } from "@/api/user.js"
import { useUserStore } from "@/stores/user.js";

let router = useRouter();
let route = useRoute();

let User = useUserStore();
let {loginAndUpdateAuthority} = User;

let loginParams = reactive({
    loginname: "",
    password: "",
})
let loginForm = ref();


let telRules = [
    { required: true, message: '请填写手机号', trigger: 'onBlur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请填写正确的手机号', trigger: 'onBlur' },
]



let onSubmit = async () => {
    console.log(1111111);
    try {
        var res = await loginForm.value.validate()
        console.log("校验成功", res);


        // var res = await doLoginAPI(loginParams);
        // console.log("登录成功",res);

        
        var res = await loginAndUpdateAuthority(loginParams);

        // // 登录成功
        // router.push("/home");

        let {ReturnUrl} = route.query;
        if(ReturnUrl){
            router.replace(decodeURIComponent(ReturnUrl))
        }else{
            router.push("/home");
        }


    } catch (err) {
        showFailToast(err.message);
    }

}





</script>

<style lang="scss" scoped>
.register {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.register-form {
    width: 80%;
    margin: 0 auto;
    margin-top: 0.5rem;

}
</style>